﻿using Microsoft.AspNetCore.Mvc;

namespace Resume.Server.Service;

[ApiExplorerSettings(GroupName = "Resume")]
public partial class DemoService : IApplicationService
{
    /// <summary>
    /// 
    /// </summary>
    private readonly SugarRepository<AppDemo> _thisRepository;
    private readonly ICurrentUser _currentUser;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="thisRepository"></param>
    /// <param name="currentUser"></param>
    public DemoService(SugarRepository<AppDemo> thisRepository, ICurrentUser currentUser)
    {
        _thisRepository = thisRepository;
        _currentUser = currentUser;
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns> 
    public async Task<RestfulResult<PageResult<AppDemo>>> GetPagesAsync(AppDemoPageParam? data)
    {
        var restful = new RestfulResult<PageResult<AppDemo>>() { Code = CodeStatus.OK };
        data ??= new AppDemoPageParam { PageNo = 1, PageSize = 10 };
        var query = await _thisRepository.AsQueryable()
            .Where(u => u.Creator == _currentUser.UserId)
            .ToPageAsync(data.PageNo, data.PageSize);

        restful.Data = query;
        return restful;
    }

    /// <summary>
    /// 所有
    /// </summary>
    /// <returns></returns>
    public async Task<RestfulResult<PageResult<AppDemo>>> GetlistAsync()
    {
        var restful = new RestfulResult<PageResult<AppDemo>>() { Code = CodeStatus.OK };
        PageParam data = new PageParam { PageNo = 1, PageSize = 100 };
        var query = await _thisRepository.AsQueryable()
             .Where(u => u.IsDisable == YesOrNot.N)
             .Where(u => u.Creator == _currentUser.UserId)
             .OrderByDescending(u => u.IsDefault)
             .Select(u => new AppDemo
             {
                 Id = u.Id,
                 Code = u.Code,
                 Name = u.Name
             })
            .ToPageAsync(data.PageNo, data.PageSize);
        restful.Data = query;
        return restful;
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork] 
    public async Task<RestfulResult> AddAsync(AppDemo data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        var isExist = await _thisRepository.IsAnyAsync(u => u.Name == data.Name || u.Code == data.Code && u.Creator == _currentUser.UserId);
        if (isExist)
        {
            restful.Message = "标题重复";
            return restful;
        }
        data.Creator = _currentUser.UserId;
        bool result = await _thisRepository.InsertAsync(data);
        if (result)
        {
            restful.Data = result;
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Message = "添加失败";
            return restful;
        }
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public async Task<RestfulResult> UpdateAsync(AppDemo data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        var isExist = await _thisRepository.IsAnyAsync(u => u.Id == data.Id && u.Creator == _currentUser.UserId);
        if (!isExist)
        {
            restful.Message = "记录不存在";
            return restful;
        }
        bool result = await _thisRepository.UpdateAsync(data, true, true, "笔记修改");
        if (result)
        {
            restful.Data = result;
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Message = "修改失败";
            return restful;
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns> 
    public async Task<RestfulResult> DeleteAsync([FromBody] EntityBaseId data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };

        if (data.Id == 0)
        {
            restful.Message = "删除失败";
            return restful;
        }
        bool isDelete = await _thisRepository.DeleteByIdAsync(data.Id);
        if (isDelete)
        {
            restful.Data = isDelete;
            restful.Message = "删除成功";
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Data = isDelete;
            restful.Message = "删除失败";
            return restful;
        }
    }
}

